import java.util.LinkedList;

public class LinkList<K> {
    class Node{
        K val;
        Node next;
        public Node(K val){
            this.val=val;
        }
    }
    Node head=null;
    public void addFist(K val){
        Node node=new Node(val);
        node.next=head;
        head=node;
    }
    public void addLast(K val){
        Node node=new Node(val);
        if(head==null){
            head=node;
        }
        Node cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }
    public void check(int index){
        if(index<0||index>size()){
            throw new InddexExpection("index 不合法");
        }
    }
    public void addIndex(int index,K val){
        try {
            check(index);
        } catch (InddexExpection e){
            e.printStackTrace();
        }
        Node node =new Node(val);
        if(index==1){
            addFist(val);
        }if(index==size()){
            addLast(val);
        }
        Node cur=head;
        while(cur.next.val==val){
            cur=cur.next;
        }
        node.next=cur.next.next;
    }
    public int size(){
        Node cur=head;
        if(head==null){
            return 0;
        }
        int count=0;
        while(cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    public static void main(String[] args) {

    }
}
